function getQueryObj() {
  const [, query] = window.location.hash.split('?');
  let queryObj: Record<string, any> = {};
  if (!query?.trim()) return queryObj;
  query.split('&').forEach((item) => {
    if (item.startsWith('token')) {
      queryObj['token'] = decodeURIComponent(item.slice(6)).replace(/\s/g, '+');
    } else {
      let [key, value] = item.split('=');
      queryObj[key] = decodeURIComponent(value).replace(/\s/g, '+');
    }
  });
  return queryObj;
}

export function isValidPhoneNumber(phoneNumber: string): boolean {
  const phoneRegex = /^1[3-9]\d{9}$/;
  return phoneRegex.test(phoneNumber);
}

/*
 * 拼接 class
 * */
const cc = (...classes: string[]): string => classes.join(' ');

/*
 * 格式化时间
 * */
const formatTime = (seconds: any): {
  formattedMinutes: string;
  formattedSeconds: string;
} => {
  const minutes = Math.floor(seconds / 60);
  const remainingSeconds = seconds % 60;
  const formattedMinutes = String(minutes).padStart(2, '0');
  const formattedSeconds = String(remainingSeconds).padStart(2, '0');
  return {formattedMinutes, formattedSeconds}
};

/*
 * 格式化大数字
 * */
const formatBigNumber = (number: string | number): string => {
  let tempNumber = number;
  if (typeof number === 'string') {
    tempNumber = parseInt(number);
  }
  if (tempNumber < 1e4) {
    return `${tempNumber}`;
  } else {
    return `${((tempNumber as number) / 1e4).toFixed(2)}W`;
  }
};

const escape2Html = (str: any) => {
  if (!str) return '';
  const arrEntities: any = {lt: '<', gt: '>', nbsp: ' ', amp: '&', quot: '"'};
  return str.replace(/&(ltlgtlnbsplamplquot);/gi, function (all: any, t: any) {
    return arrEntities[t];
  });
};

export const htm12Escape = (str: any) => {
  return str.replace(/[<>&"]/g, function (c: any) {
    // @ts-ignore
    return {
      '<': '&lt;',
      '>': '&gt;',
      '&': '&amp;',
      '"': '&quot;',
    }[c];
  });
};

export {getQueryObj, cc, formatBigNumber, escape2Html, formatTime};
